package com.random;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.Arrays;

public class Test01 {
	public static void main(String[] args)throws Exception {
		File file = new File("E:/PR/a.txt");
		RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
		int temp =  randomAccessFile.read();
		System.out.println(temp);
		//System.out.println(randomAccessFile.readChar());//和writeChar一致
		//System.out.println(randomAccessFile.readUTF());//和writeUTF一致
		
		byte[] bs = new byte[10];
		randomAccessFile.read(bs);
		System.out.println(Arrays.toString(bs));
		
		bs = new byte[10];
		randomAccessFile.seek(0);
		randomAccessFile.read(bs);
		System.out.println(Arrays.toString(bs));
	
		bs = new byte[10];
		randomAccessFile.seek(5);
		randomAccessFile.read(bs);
		System.out.println(Arrays.toString(bs));
		System.out.println(randomAccessFile.getFilePointer());
		System.out.println(randomAccessFile.length());
		
		
		//randomAccessFile.setLength(2048);//java.io.IOException: 拒绝访问。只读方式
		
		randomAccessFile = new RandomAccessFile(file, "rw");
		randomAccessFile.setLength(2048);
		randomAccessFile.setLength(2);
		
		randomAccessFile.write(71);//GB
		bs = new byte[] {100,101,102,103};
		randomAccessFile.write(bs);//GHdefg
		randomAccessFile.seek(1);
		randomAccessFile.write(bs);//Gdefgg
		randomAccessFile.seek(10);
		randomAccessFile.write(bs);//Gdefgg  defg
		
//		System.out.println(randomAccessFile.getFilePointer());
//		randomAccessFile.writeInt(99);
//		System.out.println(randomAccessFile.getFilePointer());
//		randomAccessFile.writeChar('A');
//		System.out.println(randomAccessFile.getFilePointer());
//		randomAccessFile.writeBoolean(true);
//		System.out.println(randomAccessFile.getFilePointer());
//		randomAccessFile.writeUTF("大家好");
		
		randomAccessFile.seek(14);
		System.out.println(randomAccessFile.readInt());
		randomAccessFile.seek(19);
		System.out.println(randomAccessFile.readChar());
		randomAccessFile.seek(19);
		System.out.println(randomAccessFile.readBoolean());
		randomAccessFile.seek(19);
		System.out.println(randomAccessFile.readUTF());
	}
}
